package com.ruoyi.deviceControl.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; //添加
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.xuehua.SnowflakeIdUtils;
import com.ruoyi.deviceControl.domain.DeviceControl;
import com.ruoyi.deviceControl.domain.LinkageControl;
import com.ruoyi.deviceControl.dto.LinkageControlDto;
import com.ruoyi.deviceControl.mapper.LinkageControlMapper;
import com.ruoyi.deviceControl.service.IDeviceControlService;
import com.ruoyi.deviceControl.service.ILinkageControlService;
import com.ruoyi.safety.domain.Device;
import com.ruoyi.safety.service.IDeviceService;
import com.ruoyi.system.domain.GradedAlarm;
import com.ruoyi.system.service.IGradedAlarmService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 联动控制Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-14
 */
@Service
public class LinkageControlServiceImpl extends ServiceImpl<LinkageControlMapper, LinkageControl> implements ILinkageControlService //修改
{
    @Autowired
    private LinkageControlMapper linkageControlMapper;

    @Autowired
    private IGradedAlarmService gradedAlarmService;

    @Autowired
    private IDeviceControlService deviceControlService;

    @Autowired
    private IDeviceService deviceService;

    /**
     * 查询联动控制
     * 
     * @param linkageControlId 联动控制主键
     * @return 联动控制
     */
    @Override
    public LinkageControl selectLinkageControlByLinkageControlId(String linkageControlId)
    {
        return linkageControlMapper.selectLinkageControlByLinkageControlId(linkageControlId);
    }

    /**
     * 查询联动控制列表
     * 
     * @param linkageControl 联动控制
     * @return 联动控制
     */
    @Override
    public List<LinkageControl> selectLinkageControlList(LinkageControl linkageControl)
    {
        return linkageControlMapper.selectLinkageControlList(linkageControl);
    }

    /**
     * 新增联动控制
     * 
     * @param linkageControl 联动控制
     * @return 结果
     */
    @Override
    public int insertLinkageControl(LinkageControl linkageControl)
    {
        linkageControl.setLinkageControlId(String.valueOf(new SnowflakeIdUtils(5,8).nextId()));
        linkageControl.setCreateTime(DateUtils.getNowDate());
        return linkageControlMapper.insertLinkageControl(linkageControl);
    }

    /**
     * 修改联动控制
     * 
     * @param linkageControl 联动控制
     * @return 结果
     */
    @Override
    public int updateLinkageControl(LinkageControl linkageControl)
    {
        linkageControl.setUpdateTime(DateUtils.getNowDate());
        return linkageControlMapper.updateLinkageControl(linkageControl);
    }

    /**
     * 批量删除联动控制
     * 
     * @param linkageControlIds 需要删除的联动控制主键
     * @return 结果
     */
    @Override
    public int deleteLinkageControlByLinkageControlIds(String[] linkageControlIds)
    {
        return linkageControlMapper.deleteLinkageControlByLinkageControlIds(linkageControlIds);
    }

    /**
     * 删除联动控制信息
     * 
     * @param linkageControlId 联动控制主键
     * @return 结果
     */
    @Override
    public int deleteLinkageControlByLinkageControlId(String linkageControlId)
    {
        return linkageControlMapper.deleteLinkageControlByLinkageControlId(linkageControlId);
    }

    /**
     * 查询联动控制列表by分级报警id
     */
    @Override
    public List<LinkageControlDto> selectLinkageControlListByAlarmId(String alarmId) {
        List<LinkageControl> linkageControls = linkageControlMapper.selectLinkageControlListByAlarmId(alarmId);
        List<LinkageControlDto> collect = linkageControls.stream().map(linkageControl -> {
            LinkageControlDto linkageControlDto = new LinkageControlDto();
            BeanUtils.copyProperties(linkageControl,linkageControlDto);
            if (linkageControl.getGradedAlarmId() != null && linkageControl.getGradedAlarmId() != ""){
                GradedAlarm gradedAlarm = gradedAlarmService.selectGradedAlarmByGradedAlarmId(linkageControl.getGradedAlarmId());
                linkageControlDto.setAlarmName(gradedAlarm.getOne());
            }
            if (linkageControl.getDeviceControlId() != null && linkageControl.getDeviceControlId() != ""){
                DeviceControl deviceControl = deviceControlService.selectDeviceControlByDeviceControlId(linkageControl.getDeviceControlId());
                linkageControlDto.setDeviceControlName(deviceControl.getControlName());
            }
            if (linkageControl.getDeviceId() != null && linkageControl.getDeviceId() != ""){
                Device device = deviceService.selectDeviceByDeviceId(linkageControl.getDeviceId());
                linkageControlDto.setDeviceName(device.getDeviceName());
            }
            return linkageControlDto;
        }).collect(Collectors.toList());
        return collect;
    }

    /**
     * 查询联动控制列表的设备by分级报警ids
     */
    @Override
    public String selectLinkageControlListByAlarmIds(String[] alarmIds) {
//        List<LinkageControl> linkageControls = linkageControlMapper.selectLinkageControlListByAlarmIds(alarmIds);
//        String deviceIds = "";
//        for (LinkageControl linkageControl : linkageControls) {
//            deviceIds += linkageControl.getDeviceId();
//        }
//        return deviceIds;
        List<LinkageControl> linkageControls = linkageControlMapper.selectLinkageControlListByAlarmIds(alarmIds);
        StringBuilder deviceIdsBuilder = new StringBuilder();
        boolean isFirst = true; // 用于检查是否是第一个元素，以避免在开头添加逗号

        for (LinkageControl linkageControl : linkageControls) {
            if (!isFirst) {
                deviceIdsBuilder.append(","); // 在非第一个元素前添加逗号
            }
            deviceIdsBuilder.append(linkageControl.getDeviceId());
            isFirst = false; // 标记已经不是第一个元素了
        }

        String deviceIds = deviceIdsBuilder.toString();
        return deviceIds;
    }
}
